package com.tushare.util;

import com.tushare.entity.Daily;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CalMaUtil {
    private static final int FIVE = 5;
    private static final int Ten = 10;
    private static final int MONTH = 30;
    private static final int YEAR = 365;

    public static List<Float> calFiveMa(List<Daily> dailyList) {
        return calMa(dailyList, FIVE);
    }

    public static List<Float> calTenMa(List<Daily> dailyList) {
        return calMa(dailyList, Ten);
    }

    public static List<Float> calMonthMa(List<Daily> dailyList) {
        return calMa(dailyList, MONTH);
    }

    private static List<Float> calMa(List<Daily> dailyList, int avg) {
        List<Float> closeList = dailyList.stream().map(Daily::getClose).collect(Collectors.toList());
        List<Float> maList = new ArrayList<>();
        for (int i = 0; i < dailyList.size(); i++) {
            float totalClose = 0.0f;
            List<Float> subClose = closeList.subList(i > avg ? i - avg : 0, i + 1);
            for (Float close : subClose) {
                totalClose += close;
            }
            maList.add(totalClose / subClose.size());
        }
        return maList;
    }
}
